package com.loong.musicdemo.util.httpUtil;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.widget.Toast;


import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Properties;

import static android.content.Context.MODE_PRIVATE;

public class LoginUtil {
    /**
     * 刷新登录
     *
     * @return loginCode
     */
//    public static void refreshLogin(Context context) {
//        Properties pro = new Properties();
//        final StringBuilder code = new StringBuilder();
//        try {
//            pro.load(context.getAssets().open("httpconfig.properties"));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        String url = pro.getProperty("host") + "/login/refresh";
//        HttpUtil.sendOKHttpRequest(url, new Callback() {
//            @Override
//            public void onFailure(@NotNull Call call, @NotNull IOException e) {
//                code.append("400");
//                Toast.makeText(context,"登录失败",Toast.LENGTH_SHORT).show();
//            }
//
//            @Override
//            public void onResponse(@NotNull Call call, @NotNull Response response) {
//                try {
//                    String body = Objects.requireNonNull(response.body()).string();
//                    try {
//                        JSONObject object = new JSONObject(body);
//                        code.append()
//                    } catch (JSONException e) {
//                        e.printStackTrace();
//                    }
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        });
//    }

    /**
     * 退出登录
     */
    public static void logout(Context context) {
        SharedPreferences userInfo = context.getSharedPreferences("userInfo", MODE_PRIVATE);
        SharedPreferences.Editor editor = userInfo.edit();
        if (userInfo.getString("userId", null) != null) {//当前存在登录信息
            editor.clear().apply();
        }else {//当前为未登录状态
            Toast.makeText(context,"当前未登录",Toast.LENGTH_SHORT).show();
        }
    }


    /**
     * 传入具体数据的 登录操作
     *
     * @param account 用户账号
     * @param pwd     用户密码
     * @param type    用户登录类型
     * @return loginCode
     */
    public static String login(String account, String pwd, String type, Context context) {
        Properties pro = new Properties();
        try {
            pro.load(context.getAssets().open("httpconfig.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        String host = pro.getProperty("host");
        String loginUrl = "/login";
        String url = "";
        if (type.equals("邮箱")) {
            url = host + loginUrl + "?email=" + account + "&password=" + pwd;
        } else if (type.equals("手机")) {
            url = host + loginUrl + "/cellphone?phone=" + account + "&password=" + pwd;
        }
        String code = "";
        String responseBody = HttpUtil.sendReturnOKHttpRequest(url);
        try {
            if (responseBody!=null){
                JSONObject object = new JSONObject(responseBody);
                JSONObject profile = object.getJSONObject("profile");
                SharedPreferences userInfo = context.getSharedPreferences("userInfo", MODE_PRIVATE);
                SharedPreferences.Editor editor = userInfo.edit();
                editor.putString("userId",profile.getString("userId"));
                editor.putString("nickName",profile.getString("nickname"));
                editor.putInt("gender",profile.getInt("gender"));
                editor.putString("account",account);
                editor.putString("type",type);
                editor.putString("pwd",pwd);
                editor.apply();
                code = object.getString("code");
            }else {
                code = "400";
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return code;
    }
}
